------------------------------------------------------------------------------------------------------------
-- Zone Control Detail Bar Global Variables
------------------------------------------------------------------------------------------------------------
EA_Window_ZoneControl.Settings = {}

------------------------------------------------------------------------------------------------------------
-- Zone Control Detail Bar Local Variables
------------------------------------------------------------------------------------------------------------

local DETAIL_WINDOW_NAME = "EA_Window_ZoneControlDetailWindow"
local DETAIL_WINDOW_WIDTH = 247             -- The full width of the detail window (including spacing -- should match the size in the xml file.)
local DETAIL_WINDOW_BAR_HEIGHT = 28         -- The height of a detail bar
local DETAIL_WINDOW_OUTER_BORDER = 10       -- Space between contents of window and window border
local DETAIL_WINDOW_INNER_BORDER = 3        -- Vertical spacing between different bars
local DETAIL_WINDOW_CLOSE_BUTTON_OFFSET = 12 -- How much to offset the close button from the main window. Should match the value of the Background anchor offset in the xml file.

-- These numbers must match the id numbers in the xml
local DETAIL_BAR_OBJECTIVE = 1
local DETAIL_BAR_SCENARIO = 2
local DETAIL_BAR_SKIRMISH = 3
local DETAIL_BAR_PVE = 4
local DETAIL_BAR_PRIOR_TIER = 5

local DETAIL_BARS =
{
    DETAIL_BAR_OBJECTIVE,
    DETAIL_BAR_SCENARIO,
    DETAIL_BAR_SKIRMISH,
    DETAIL_BAR_PVE,
    DETAIL_BAR_PRIOR_TIER,
}

local layoutRegistered = false
local priorTierBarIndex = DETAIL_BAR_PRIOR_TIER

local DETAIL_BAR_CONTRIBUTIONS_NONCITY =
{
    -- Tier 1
    [1] =
    {
        [DETAIL_BAR_OBJECTIVE]    = 40,
        [DETAIL_BAR_SCENARIO]     = 40,
        [DETAIL_BAR_SKIRMISH]     = 10,
        [DETAIL_BAR_PVE]          = 10,
        [DETAIL_BAR_PRIOR_TIER]   = 0,
    },
    
    -- Tier 2
    [2] =
    {
        [DETAIL_BAR_OBJECTIVE]    = 35,
        [DETAIL_BAR_SCENARIO]     = 35,
        [DETAIL_BAR_SKIRMISH]     = 10,
        [DETAIL_BAR_PVE]          = 10,
        [DETAIL_BAR_PRIOR_TIER]   = 10,
    },
    
    -- Tier 3
    [3] =
    {
        [DETAIL_BAR_OBJECTIVE]    = 35,
        [DETAIL_BAR_SCENARIO]     = 35,
        [DETAIL_BAR_SKIRMISH]     = 10,
        [DETAIL_BAR_PVE]          = 10,
        [DETAIL_BAR_PRIOR_TIER]   = 10,
    },
    
    -- Tier 4
    [4] =
    {
        [DETAIL_BAR_OBJECTIVE]    = 45,
        [DETAIL_BAR_SCENARIO]     = 25,
        [DETAIL_BAR_SKIRMISH]     = 20,
        [DETAIL_BAR_PVE]          = 5,
        [DETAIL_BAR_PRIOR_TIER]   = 5,
    }
}

local DETAIL_BAR_CONTRIBUTIONS_CITY =
{
    [DETAIL_BAR_OBJECTIVE]    = 55,
    [DETAIL_BAR_SCENARIO]     = 5,
    [DETAIL_BAR_SKIRMISH]     = 30,
    [DETAIL_BAR_PVE]          = 10,
    [DETAIL_BAR_PRIOR_TIER]   = 0,
}

------------------------------------------------------------------------------------------------------------
-- Zone Control Detail Bar Functions
------------------------------------------------------------------------------------------------------------

function EA_Window_ZoneControl.GetDetailTextString(barType)
    local text = L""
    if (barType == DETAIL_BAR_OBJECTIVE) then
        text = GetStringFromTable("WorldControl", StringTables.WorldControl.TEXT_CONTROL_DETAIL_OBJECTIVE)
    elseif (barType == DETAIL_BAR_SCENARIO) then
        text = GetStringFromTable("WorldControl", StringTables.WorldControl.TEXT_CONTROL_DETAIL_SCENARIO)
    elseif (barType == DETAIL_BAR_SKIRMISH) then
        text = GetStringFromTable("WorldControl", StringTables.WorldControl.TEXT_CONTROL_DETAIL_SKIRMISH)
    elseif (barType == DETAIL_BAR_PVE) then
        text = GetStringFromTable("WorldControl", StringTables.WorldControl.TEXT_CONTROL_DETAIL_PVE)
    elseif (barType == DETAIL_BAR_PRIOR_TIER) then
        text = GetStringFromTable("WorldControl", StringTables.WorldControl.TEXT_CONTROL_DETAIL_PRIOR_TIER)
    end
    return text
end

function EA_Window_ZoneControl.GetDetailTooltipString(barType)
    local text = L""
    if (barType == DETAIL_BAR_OBJECTIVE) then
        text = GetStringFromTable("WorldControl", StringTables.WorldControl.TOOLTIP_CONTROL_DETAIL_OBJECTIVE)
    elseif (barType == DETAIL_BAR_SCENARIO) then
        text = GetStringFromTable("WorldControl", StringTables.WorldControl.TOOLTIP_CONTROL_DETAIL_SCENARIO)
    elseif (barType == DETAIL_BAR_SKIRMISH) then
        text = GetStringFromTable("WorldControl", StringTables.WorldControl.TOOLTIP_CONTROL_DETAIL_SKIRMISH)
    elseif (barType == DETAIL_BAR_PVE) then
        text = GetStringFromTable("WorldControl", StringTables.WorldControl.TOOLTIP_CONTROL_DETAIL_PVE)
    elseif (barType == DETAIL_BAR_PRIOR_TIER) then
        text = GetStringFromTable("WorldControl", StringTables.WorldControl.TOOLTIP_CONTROL_DETAIL_PRIOR_TIER)
    end
    return text
end

function EA_Window_ZoneControl.InitializeDetailWindow()

    EA_Window_ZoneControl.LoadSettings()
    
    local barLastWindowName = ""
    for barIndex, barType in ipairs(DETAIL_BARS) do

        local barWindowName = DETAIL_WINDOW_NAME.."Bar"..barIndex
        
        local barLabelName = barWindowName.."Name"
        local textOrderName = barWindowName.."OrderText"
        local textDestructionName = barWindowName.."DestructionText"
        
        local barLabelText = EA_Window_ZoneControl.GetDetailTextString(barType)
        
        WindowClearAnchors(barWindowName)
        if (barLastWindowName == "") then
            WindowAddAnchor(barWindowName, "topleft", DETAIL_WINDOW_NAME, "topleft", DETAIL_WINDOW_OUTER_BORDER, DETAIL_WINDOW_OUTER_BORDER + DETAIL_WINDOW_CLOSE_BUTTON_OFFSET)
        else
            WindowAddAnchor(barWindowName, "bottomleft", barLastWindowName, "topleft", 0, DETAIL_WINDOW_INNER_BORDER)
        end
        LabelSetText(barLabelName, barLabelText)
        
        DefaultColor.LabelSetTextColor(textOrderName, DefaultColor.RealmColors[GameData.Realm.ORDER])
        DefaultColor.LabelSetTextColor(textDestructionName, DefaultColor.RealmColors[GameData.Realm.DESTRUCTION])
            
        barLastWindowName = barWindowName
        
    end
    
    EA_Window_ZoneControl.SetBarsAndTextShowing()
    EA_Window_ZoneControl.SetDetailWindowDimensions(#DETAIL_BARS)
    WindowSetAlpha(DETAIL_WINDOW_NAME, 1.0)
    WindowRegisterEventHandler(DETAIL_WINDOW_NAME, SystemData.Events.ZONE_CONTROL_DETAIL_AMOUNTS_UPDATED, "EA_Window_ZoneControl.UpdateDetailAmounts")
    WindowRegisterEventHandler(DETAIL_WINDOW_NAME, SystemData.Events.PLAYER_ZONE_CHANGED,                 "EA_Window_ZoneControl.UpdateDetailAmounts")

end

function EA_Window_ZoneControl.LoadSettings()
    -- Set initial settings if this is the first time this module has been loaded
    if (EA_Window_ZoneControl.Settings.isInBarMode == nil) then
        EA_Window_ZoneControl.Settings.isInBarMode = true
    end
    if (EA_Window_ZoneControl.Settings.isTemporarilyHidden == nil) then
        EA_Window_ZoneControl.Settings.isTemporarilyHidden = false
    end
end

function EA_Window_ZoneControl.SetDetailWindowDimensions(numberOfBars)
    local windowHeight = (2 * DETAIL_WINDOW_OUTER_BORDER) + (numberOfBars * DETAIL_WINDOW_BAR_HEIGHT) + ((numberOfBars - 1) * DETAIL_WINDOW_INNER_BORDER) + DETAIL_WINDOW_CLOSE_BUTTON_OFFSET
    WindowSetDimensions(DETAIL_WINDOW_NAME, DETAIL_WINDOW_WIDTH, windowHeight)
end

function EA_Window_ZoneControl.SetBarsAndTextShowing()
    for barIndex, _ in ipairs(DETAIL_BARS) do
        local barWindowName = DETAIL_WINDOW_NAME.."Bar"..barIndex
        local barOrderName = barWindowName.."OrderBar"
        local barDestructionName = barWindowName.."DestructionBar"
        local textOrderName = barWindowName.."OrderText"
        local textDestructionName = barWindowName.."DestructionText"
        
        local isInBarMode = EA_Window_ZoneControl.Settings.isInBarMode
        WindowSetShowing(barOrderName, isInBarMode)
        WindowSetShowing(barDestructionName, isInBarMode)
        WindowSetShowing(textOrderName, not isInBarMode)
        WindowSetShowing(textDestructionName, not isInBarMode)
    end
end

function EA_Window_ZoneControl.IsInCity()
    -- First test if player is in a non-contested city
    if (GameData.Player.City.id ~= 0) then
        return true
    end
    
    -- Now test if player is in a city instance
    local instanceIdData = GetCityInstanceId()
    return (instanceIdData.instanceId > 1)
end

function EA_Window_ZoneControl.GetAllowedToShowDetailWindow()
    -- Temp code to hide detail window in cities
    if (EA_Window_ZoneControl.IsInCity()) then
        return false
    end
    
    local zoneData = nil
    if (GameData.Player.zone > 0)
    then
        local parentMap = MapGetParentMap( GameDefs.MapLevel.ZONE_MAP, GameData.Player.zone )
        if parentMap ~= nil and parentMap.mapNumber < GameData.ExpansionMapRegion.FIRST
        then
            zoneData = GetCampaignZoneData( GameData.Player.zone )    
        end
    end
    
    return (zoneData ~= nil)
end

function EA_Window_ZoneControl.GetShouldShowPriorTier()
    return ((EA_Window_ZoneControl.IsInCity() == false) and (GetZoneTier() ~= 1))
end

function EA_Window_ZoneControl.GetDetailAmounts(realm)
    local returnArray = {}
    
    returnArray[DETAIL_BAR_SCENARIO], returnArray[DETAIL_BAR_SKIRMISH], returnArray[DETAIL_BAR_OBJECTIVE], returnArray[DETAIL_BAR_PVE], returnArray[DETAIL_BAR_PRIOR_TIER] = GetZoneControlDetailAmounts(realm)
    return returnArray
end

function EA_Window_ZoneControl.SetBarWidthsAndTextLabels(shouldShowPriorTier)

    local orderControl = EA_Window_ZoneControl.GetDetailAmounts(GameData.Realm.ORDER)
    local destructionControl = EA_Window_ZoneControl.GetDetailAmounts(GameData.Realm.DESTRUCTION)

    local totalBarWidth = DETAIL_WINDOW_WIDTH - (2 * DETAIL_WINDOW_OUTER_BORDER) - DETAIL_WINDOW_CLOSE_BUTTON_OFFSET
    for barIndex, barType in ipairs(DETAIL_BARS) do

        if ((barType ~= DETAIL_BAR_PRIOR_TIER) or (shouldShowPriorTier)) then
        
            local barWindowName = DETAIL_WINDOW_NAME.."Bar"..barIndex
            local barOrderName = barWindowName.."OrderBar"
            local barDestructionName = barWindowName.."DestructionBar"
            local textOrderName = barWindowName.."OrderText"
            local textDestructionName = barWindowName.."DestructionText"
            
            local orderWidth = math.floor(totalBarWidth * orderControl[barType] / 100.0)
            local destructionWidth = math.floor(totalBarWidth * destructionControl[barType] / 100.0)
            
            WindowSetDimensions(barWindowName, totalBarWidth, DETAIL_WINDOW_BAR_HEIGHT)
            
            WindowSetDimensions(barOrderName, orderWidth, DETAIL_WINDOW_BAR_HEIGHT)
            WindowSetDimensions(barDestructionName, destructionWidth, DETAIL_WINDOW_BAR_HEIGHT)
                            
            LabelSetText(textOrderName, GetStringFormat(StringTables.Default.GENERIC_PERCENTAGE, { orderControl[barType] }))
            LabelSetText(textDestructionName, GetStringFormat(StringTables.Default.GENERIC_PERCENTAGE, { destructionControl[barType] }))
                
        end
            
    end
    
end

function EA_Window_ZoneControl.ShowPriorTier(shouldShowPriorTier)
    local priorTierBarWindowName = DETAIL_WINDOW_NAME.."Bar"..priorTierBarIndex
            
    if (shouldShowPriorTier) then
        WindowSetShowing(priorTierBarWindowName, true)
        EA_Window_ZoneControl.SetDetailWindowDimensions(#DETAIL_BARS)
    else
        WindowSetShowing(priorTierBarWindowName, false)
        EA_Window_ZoneControl.SetDetailWindowDimensions(#DETAIL_BARS - 1)
    end
end

function EA_Window_ZoneControl.UpdateDetailWindow()
    local shouldShowPriorTier = EA_Window_ZoneControl.GetShouldShowPriorTier()

    EA_Window_ZoneControl.ShowPriorTier(shouldShowPriorTier)
    EA_Window_ZoneControl.SetBarWidthsAndTextLabels(shouldShowPriorTier)
end
 
function EA_Window_ZoneControl.ShowDetailWindow()
    if (EA_Window_ZoneControl.GetAllowedToShowDetailWindow()) then
        WindowSetShowing(DETAIL_WINDOW_NAME, true)
    end
end

function EA_Window_ZoneControl.HideDetailWindow(isTemporaryHide)
    WindowSetShowing( DETAIL_WINDOW_NAME, false )
    if (isTemporaryHide == true) then
        EA_Window_ZoneControl.Settings.isTemporarilyHidden = true
    else
        EA_Window_ZoneControl.Settings.isTemporarilyHidden = false
    end
end

function EA_Window_ZoneControl.UpdateDetailAmounts()
    local allowedToShow = EA_Window_ZoneControl.GetAllowedToShowDetailWindow()
    if (WindowGetShowing(DETAIL_WINDOW_NAME)) then
        if (allowedToShow) then
            EA_Window_ZoneControl.UpdateDetailWindow()
        else
            EA_Window_ZoneControl.HideDetailWindow(true)
        end
    -- If we were temporarily hidden, and we're now in a place where we're allowed to show, then show the window
    elseif (EA_Window_ZoneControl.Settings.isTemporarilyHidden and allowedToShow) then
        EA_Window_ZoneControl.ShowDetailWindow()
    end
end

function EA_Window_ZoneControl.OnDetailBarMouseOver()
    local barType = WindowGetId(SystemData.ActiveWindow.name)
    local contributionToOverallZoneControl = 0
    
    if (EA_Window_ZoneControl.IsInCity()) then
        contributionToOverallZoneControl = DETAIL_BAR_CONTRIBUTIONS_CITY[barType]
    else
        local currentTier = GetZoneTier()
        if (currentTier >= 1 and currentTier <= 4) then
            contributionToOverallZoneControl = DETAIL_BAR_CONTRIBUTIONS_NONCITY[currentTier][barType]
        end
    end
    
    Tooltips.CreateTextOnlyTooltip( SystemData.ActiveWindow.name )
    Tooltips.SetTooltipText( 1, 1, EA_Window_ZoneControl.GetDetailTextString(barType) )
    Tooltips.SetTooltipColorDef( 1, 1, Tooltips.COLOR_HEADING )
    Tooltips.SetTooltipText( 2, 1, EA_Window_ZoneControl.GetDetailTooltipString(barType) )
    Tooltips.SetTooltipColorDef( 2, 1, Tooltips.COLOR_BODY )
    Tooltips.SetTooltipText( 3, 1, GetStringFormatFromTable("WorldControl", StringTables.WorldControl.TOOLTIP_CONTROL_CONTRIBUTION, { L""..contributionToOverallZoneControl }) )
    Tooltips.SetTooltipColorDef( 3, 1, Tooltips.COLOR_HEADING )
    Tooltips.Finalize()
    Tooltips.AnchorTooltip(Tooltips.ANCHOR_LEFT)
end

function EA_Window_ZoneControl.OnRButtonUp()
    EA_Window_ContextMenu.CreateContextMenu( SystemData.ActiveWindow.name )
    local isInBarMode = EA_Window_ZoneControl.Settings.isInBarMode
    EA_Window_ContextMenu.AddMenuItem( GetStringFromTable("WorldControl", StringTables.WorldControl.TEXT_DETAIL_WINDOW_GRAPHICAL_VIEW), EA_Window_ZoneControl.SetToBarMode, isInBarMode, true )
    EA_Window_ContextMenu.AddMenuItem( GetStringFromTable("WorldControl", StringTables.WorldControl.TEXT_DETAIL_WINDOW_TEXTUAL_VIEW), EA_Window_ZoneControl.SetToTextMode, not isInBarMode, true )
    EA_Window_ContextMenu.Finalize()
end

function EA_Window_ZoneControl.SetToBarMode()
    EA_Window_ZoneControl.Settings.isInBarMode = true
    EA_Window_ZoneControl.SetBarsAndTextShowing()
end

function EA_Window_ZoneControl.SetToTextMode()
    EA_Window_ZoneControl.Settings.isInBarMode = false
    EA_Window_ZoneControl.SetBarsAndTextShowing()
end

function EA_Window_ZoneControl.OnShown()
    EA_Window_ZoneControl.Settings.isTemporarilyHidden = false
    EA_Window_ZoneControl.UpdateDetailWindow()
    if (not layoutRegistered) then
        LayoutEditor.RegisterWindow( DETAIL_WINDOW_NAME,
                                     GetStringFromTable( "HUDStrings", StringTables.HUD.LABEL_HUD_EDIT_ZONE_CONTROL_DETAIL_NAME ),
                                     GetStringFromTable( "HUDStrings", StringTables.HUD.LABEL_HUD_EDIT_ZONE_CONTROL_DETAIL_DESC ),
                                     false, false,
                                     false, nil )
        layoutRegistered = true
    end
end

function EA_Window_ZoneControl.OnHidden()
    if (layoutRegistered) then
        LayoutEditor.UnregisterWindow( DETAIL_WINDOW_NAME )
        layoutRegistered = false
    end
end
